package com.taojie.graduation.controller;

import com.taojie.graduation.domain.Brand;
import com.taojie.graduation.service.api.BrandService;
import com.taojie.graduation.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@CrossOrigin
@Api(tags = {"品牌接口"})
@RequestMapping("brand")
public class BrandController {
    @Resource
    private BrandService brandService;

    @ApiOperation("分页查询")
    @GetMapping("{page}/{size}")
    public Result page(@PathVariable("page") int page, @PathVariable("size") int size, @RequestParam(value = "query", required = false) String query){
        return brandService.findBrandPage(page, size, query);
    }

    @ApiOperation("根据id查询品牌")
    @GetMapping("{id}")
    public Result findById(@PathVariable("id") Integer id){
        return brandService.findBrandById(id);
    }

    @ApiOperation("根据id更新品牌")
    @PutMapping("{id}")
    public Result updateById(@PathVariable("id") Integer id,@RequestBody Brand brand){
        brand.setId(id);
        return brandService.updateById(brand);
    }

    @ApiOperation("根据id删除品牌")
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable("id")Integer id){
        return brandService.deleteById(id);
    }

    @ApiOperation("添加品牌")
    @PostMapping
    public Result addr(@RequestBody Brand brand) {
        return brandService.add(brand);
    }
}
